/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package modelos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author usuario
 */
public abstract class ModeloDB{
    protected Connection conexion;

    abstract public boolean registar();
    abstract public boolean  consultar();
    abstract public boolean eliminar();
    abstract public Vector<ModeloDB> listar();
    
    public void conectar(){
        try {
            
            conexion = DriverManager.getConnection("jdbc:postgresql://localhost:5433/BD4-1", "postgres", "20187031");
            conexion.setAutoCommit(false);
        } catch (SQLException ex) {
            Logger.getLogger(ModeloDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void desconectar(){
        try {
            conexion.setAutoCommit(true);
            conexion.close();
            
        } catch (SQLException ex) {
            Logger.getLogger(ModeloDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    protected final PreparedStatement crearPreparedStatement(String sql){
        PreparedStatement stmt = null;

        try {
            stmt = conexion.prepareStatement(sql);
        } catch (SQLException ex) {
            Logger.getLogger(ModeloDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    
        return stmt;
    }    

    protected final ResultSet crearQuery(String sql){

        Statement stm = null;
        ResultSet rs = null;
        try {
            stm = conexion.createStatement();
            rs = stm.executeQuery(sql);            
        } catch (SQLException ex) {
            Logger.getLogger(ModeloDB.class.getName()).log(Level.SEVERE, null, ex);
        }

        return rs;
    }

    protected final boolean ejeutarQueryGenerico (String sql){
	boolean sw=false;
	Statement stm=null;
        try {
            stm = conexion.createStatement();
            if(stm.executeUpdate(sql)>0)
                sw=true;
        } catch (SQLException ex) {
            Logger.getLogger(ModeloDB.class.getName()).log(Level.SEVERE, null, ex);
        }
	return sw;
    }

    protected final boolean  ejecutarQueryPrepare(PreparedStatement pstmt){
        try {
            pstmt.executeUpdate();
            return true;
         } catch (SQLException ex) {
            // Logger.getLogger(ModeloDB.class.getName()).log(Level.SEVERE, null, ex);
            return false;            
         }
    }


     protected final ResultSet ejecutarQuery(PreparedStatement pstmt){

         ResultSet rs =null;
         try {
            rs = pstmt.executeQuery();
         } catch (SQLException ex) {
            Logger.getLogger(ModeloDB.class.getName()).log(Level.SEVERE, null, ex);
         }
         return rs;
    }

    public final int contarRegistros(String tabla){

        int cantidad = -1;
        try {
            ResultSet rs = crearQuery("select count(*) from "+tabla);
            if(rs.next())
                cantidad = rs.getInt(1);
        } catch (SQLException ex) {
            Logger.getLogger(ModeloDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(cantidad);
        return cantidad;
    }




  

}
